package com.jcx.ldzj.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jcx.ldzj.entity.DictionariesData;
import com.jcx.ldzj.entity.DictionariesType;
import com.jcx.ldzj.service.IDictionariesDataService;
import com.jcx.ldzj.service.IDictionariesTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 码值表 前端控制器
 * </p>
 *
 * @author ld
 * @since 2021-07-19
 */
@RestController
@Slf4j
@AllArgsConstructor
@RequestMapping("/dictionaries-data")
@Api(value = "DictionariesDataController", tags = "DictionariesDataController")
public class DictionariesDataController extends ApiController {
    @Autowired
    private IDictionariesDataService iDictionariesDataService;
    @Autowired
    private IDictionariesTypeService iDictionariesTypeService;

    /**
     * 列表
     *
     * @param pageNum          当前页
     * @param pageSize         页大小
     * @param dictionariesData
     * @return 自定义分页案例，这种更加传统，也可以用代码生成的方式直接java代码写条件，两种没什么区别
     */
    @GetMapping("/list")
    @RequiresPermissions("basic:dictionariesdata:page")
    @ApiOperation(value = "列表", notes = "传入DictionariesData对象")
    public R<IPage<DictionariesData>> list(Integer pageNum, Integer pageSize, DictionariesData dictionariesData) {
        IPage<DictionariesData> page = iDictionariesDataService.getList(new Page<>(pageNum, pageSize), dictionariesData);
        return R.ok(page);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @RequiresPermissions("basic:dictionariesdata:view")
    @ApiOperation(value = "详情", notes = "传入id")
    public R<DictionariesData> view(@PathVariable("id") String id) {
        DictionariesData user = iDictionariesDataService.getById(id);
        return R.ok(user);
    }

    /**
     * 新增
     *
     * @param dictionariesData
     * @return
     */
    @PostMapping
    @RequiresPermissions("basic:dictionariesdata:add")
    @ApiOperation(value = "新增", notes = "传入DictionariesData")
    public R add(@Valid @RequestBody DictionariesData dictionariesData) {
        return this.success(iDictionariesDataService.save(dictionariesData));
    }


    /**
     * 修改
     *
     * @param dictionariesData
     * @return
     */
    @PutMapping
    @ApiOperation(value = "修改", notes = "传入DictionariesData")
    @RequiresPermissions("basic:dictionariesdata:update")
    public R edit(@Valid @RequestBody DictionariesData dictionariesData) {
        return this.success(iDictionariesDataService.updateById(dictionariesData));
    }

    /**
     * 删除
     */
    @DeleteMapping("/{ids}")
    @RequiresPermissions("basic:dictionariesdata:delete")
    @ApiOperation(value = "删除", notes = "传入ids")
    public R del(@ApiParam(value = "主键集合", required = true) @PathVariable("ids") String[] ids) {
        return this.success(iDictionariesDataService.removeByIds(Arrays.asList(ids)));
    }

    /**
     * 根据传入码值类型查询字典
     *
     * @param type
     * @return
     */
    @GetMapping("/dicdataByType")
    @ApiOperation(value = "根据传入码值类型查询字典", notes = "传入码值类型")
    public R<List<DictionariesData>> dicdataByType(@Param("type") String type) {
        DictionariesType dictionariesType = iDictionariesTypeService.getdictByType(type);
        if (dictionariesType == null) {
            return R.ok(null);
        }
        List<DictionariesData> dicdataByType = iDictionariesDataService.getDicdataByType(dictionariesType.getId());
        return R.ok(dicdataByType);
    }
}
